package com.graduation.controller;

import com.graduation.config.ObjectConfig;
import com.graduation.entity.Goods;
import com.graduation.entity.Recommend;
import com.graduation.service.GoodsService;
import com.graduation.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    /**
     * 添加商品
     * @return
     */
    @PostMapping("/addGood.do")
    public ModelAndView addGoods(Goods goods){
        ModelAndView mv = new ModelAndView();
        Integer res = goodsService.queryGoodsIsExists(goods.getGoodsType());
        //添加父表中的数据
        if(res != 1){
            goodsService.insertGoods(goods.getGoodsType(), StringUtil.findGoodsType(goods.getGoodsType()));
        }
        //添加商品成功
        Integer result = goodsService.addGoods(goods);
        if(result == 1){
            mv.setViewName("index");
            return mv;
        }
        mv.setViewName("goodsAdd");
        return mv;
    }

    /**
     * 查询商品信息
     */
    @RequestMapping("/queryCommend.do")
    public List<Recommend> queryRecommend(){
        List<Recommend> recommends = goodsService.queryGoodsRecommend();
        return recommends;
    }

    /**
     * 展示商品详情
     */
    @RequestMapping("/compare.do")
    public ModelAndView goCompare(String id){
        ModelAndView mv = new ModelAndView();
        mv.addObject("id",id);
        mv.setViewName("compare");
        return mv;
    }

    /**
     * 购物
     * @param id
     * @return
     */
    @RequestMapping("/goSingle.do")
    public ModelAndView goSingleProduct(String id){
        ModelAndView mv = new ModelAndView();
        Recommend recommend = goodsService.queryRecommendById(id);
        mv.addObject("recommend",recommend);
        List<Recommend> recommends = goodsService.queryGoodsRecommend();
        mv.addObject("res",recommends);
        mv.setViewName("single-product");
        return mv;
    }

    /**
     * 查询最新发布的商品
     * @return
     */
    @RequestMapping("/queryNewGoods.do")
    public List<Recommend> queryNewGoods(){
        List<Recommend> recommends = goodsService.queryNewUpGoods();
        return recommends;
    }

    /**
     * 浏览该平台得全部商品
     * @return
     */
    @GetMapping("/browser.do")
    public ModelAndView browseGoods(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(ObjectConfig.class);
        ModelAndView mv = (ModelAndView) ac.getBean("createMandV");
        List<Goods> goods = goodsService.showGoods();
        mv.addObject("goods",goods);
        mv.setViewName("shop");
        return mv;
    }
}
